﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FacebookApp
{
    public partial class Admins_Publisher : System.Web.UI.Page
    {
        private static bool Insert;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["folder"] = "Publisher";
                BindGrid();
            }
        }

        private void BindGrid()
        {
            grdPublisher.DataSource = PublisherService.Publisher_GetByAll();
            grdPublisher.DataBind();
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            pnShow.Visible = false;
            pnUpdate.Visible = true;
            
            ControlClass.ResetControlValues(this);
            Session["upload"] = null;
            Insert = true;
        }

        protected void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                DataGridItem item = default(DataGridItem);
                for (int i = 0; i < grdPublisher.Items.Count; i++)
                {
                    item = grdPublisher.Items[i];
                    if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item)
                    {
                        if (((CheckBox)item.FindControl("ChkSelect")).Checked)
                        {
                            string strId = item.Cells[1].Text;
                            try
                            {
                                PublisherService.Publisher_Delete(strId);
                            }
                            catch
                            {
                            }
                        }
                    }
                }
                grdPublisher.CurrentPageIndex = 0;
                BindGrid();
            }
            catch
            {
            }
        }

        protected void grdPublisher_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            var strCA = new PublisherInfo();
            strCA.Id = e.CommandArgument.ToString();
            if (e.CommandName == "Edit")
            {
                Publisher dt = PublisherService.Publisher_GetById(strCA.Id);
                txtId.Text = dt.Id.ToString();
                txtName.Text = dt.Name.ToString();
                txtDecription.Text = dt.Decription.ToString();
                pnUpdate.Visible = true;
                pnShow.Visible = false;
            }
            else
            {
                PublisherService.Publisher_Delete(strCA.Id);
                BindGrid();
            }
        }

        protected void lblBack_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
            pnShow.Visible = true;
            Insert = false;
        }

        protected void lblUpdate_Click(object sender, EventArgs e)
        {
            string image = Session["upload"] == null ? "" : Session["upload"].ToString();

            #region[TestInput]

            //if (txtId.Text.Trim().Equals(""))
            //{
            //    WebMsgBox.Show("Id không thể trống !");
            //    txtId.Focus();
            //    return;
            //}
            if (txtName.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Name không thể trống !");
                txtName.Focus();
                return;
            }
            if (txtDecription.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Decription không thể trống !");
                txtDecription.Focus();
                return;
            }

            #endregion

            var obj = new PublisherInfo();
            obj.Id = txtId.Text;
            obj.Name = txtName.Text;
            obj.Decription = txtDecription.Text;
            if (Insert)
            {
                PublisherService.Publisher_Insert(obj);
            }
            else
            {
                PublisherService.Publisher_Update(obj);
            }
            BindGrid();
            pnShow.Visible = true;
            pnUpdate.Visible = false;
            Insert = false;
        }

        protected void grdPublisher_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdPublisher.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void grdPublisher_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
            {
                if (itemType == ListItemType.Header)
                {
                    object checkBox = e.Item.FindControl("chkSelectAll");
                    if ((checkBox != null))
                    {
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
                    }
                }
                else
                {
                    string tableRowId = grdPublisher.ClientID + "_row" + e.Item.ItemIndex.ToString();
                    e.Item.Attributes.Add("id", tableRowId);
                    object checkBox = e.Item.FindControl("chkSelect");
                    if ((checkBox != null))
                    {
                        e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
                        e.Item.Attributes.Add("onMouseOut", "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex + ")");
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex + ")");
                    }
                }
            }
        }
    }
}
